class FirstPage extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            list: [],
            thumburl: '',
            name: '',
            showchose:!1,
            id: '0',
            partcount: 1,
            parts: [{ src: '' }, { src: '' }, { src: '' }],
            categorylist: [],
            select_swiperlink: { name: '请选择' },
            swiperimglist: [],
            keyword:'',
            category_select_list: [],
            recommendtablehtml: [],
            recommendlist: [],
            goods_select_html: [],
            currentswipertype: 0,
            swiperlinkhtml: [],
            swipertablehtml: [],
            swiperimghtml: [],
            subtitle: '',
            totalamount:0,
            roomlist:[],
            roomdlist:[],
            exportlist:[],
            choselist:[],
            showpage:!1,
            // exportlist1:[
            //     {
            //         room:{
            //             name:'',
            //             id:''
            //         },
            //         wjtotal:0,
            //         cptotal:0,
            //         cptotalcount:0,
            //         amount:0,
            //         wj:[{
            //             pid: '',
            //             cid: '-1',
            //             cid1: '-1',
            //             cname: '',
            //             height: "",
            //             width: "",
            //             deep: "",
            //             singleprice: 0,
            //             count: '',
            //             amount: '',
            //             unit: '',
            //             lmsm: '',
            //             mbcz: '',
            //             gtcz: '',
            //         }
                        
            //         ],
            //         cp:[
            //             {
            //                 pid: '',
            //                 cid: '-1',
            //                 cid1: '-1',
            //                 cname: '',
            //                 height: "",
            //                 width: "",
            //                 deep: "",
            //                 singleprice: 0,
            //                 count: '',
            //                 amount: '',
            //                 unit: '',
            //                 lmsm: '',
            //                 mbcz: '',
            //                 gtcz: '',

            //             }
            //         ]
            //     }
            // ],
            swiper: {
                imgs: [],
                links: []
            },
            fileimgshow: !0,
            imgstate: '',
            imgsrc: '',
            to: '',
        };
    }


    ccheck(){
        let pwd = prompt('请输入密码')
        dz_category_checkpwd(md5(pwd + Constant.SALT),res=>{
            if(res.data==true){
                this.freshlist()
            }else{
                //this.ccheck()
                window.location.href='./dz_main.html'
            }
        })
    }

    componentDidMount() {
        $('.modal1').hide()
        let that = this
        let t = GetQueryString('t') 
        this.setState({t},()=>{
             
        haspwd(res => {
            if (res.data == true) {
                this.ccheck()
            } else {
                this.freshlist()
            }
        })
           // this.ccheck()
            $("#input").keydown(function (e){
                // 当 keyCode 是13时,是回车操作
               if (e.keyCode == 13){
                // 编写业务代码...
                haspwd(res => {
                    if (res.data == true) {
                        that.ccheck()
                    } else {
                        this.freshlist()
                    }
                })
               
               
                // 取消之前的keydown事件
                  e.preventDefault();
               }
            })

        })
      

    }
    freshlist() {
        this.setState({showpage:!0,roomdlist:[]})
        
        let {t,keyword} = this.state,that = this
        dz_getprojectdlist({pool:t==1?'XD':'QD',keyword},res=>{
            if (res.issuccess == 1) {
                this.setState({roomdlist:res.list},()=>{
                    res.list.forEach(x=>{
                        laydate.render({
                            elem: '#test'+x.id, //指定元素
                            done: function (value, date, endDate) {
                               // that.setState({ enddate: value })
                               
                               console.log('112233');

                               dz_updatetime({id:x.id,date:new Date(value).getTime()/1000 },res=>{
                                that.freshlist()

                               })
                            }
                        });
                    })
                })}
        })
    }



    

    /**新建 */
    add() {
       // $('.modal1').show()
        // var name = prompt('请输入房间名称')
        // if (name != '') {
        //     aoedzroom({ name }, res => {
        //         if (res.issuccess == 1) {
        //             this.freshlist()
        //         } else {
        //             alert(res.msg)
        //         }
        //     })
        // }

       // this.setState({showchose:!0})
    }


    // 编辑
    edit(x) {
        var name = prompt('请编辑房间名称', x.name)
        if (name != ''&&name!=null&&name!=x.name) {
          //  console.log(name,'name');

          dz_projectdetail_add({ selectid: x.id, name }, res => {
                if (res.issuccess == 1) {
                 //   alert('修改成功')
                    this.freshlist()
                } else {
                    alert(res.msg)
                }
            })
        }

        // 
    }
    tosub(x) {
        window.location.href = "./dz_subcate.html?id=" + x.id + "&name=" + x.name;
    }
    // 排序
   
  


    // 删除
    del(e, cid) {
        this.setState({ delid: e, newcid: cid }, () => {
            $('#myModal').modal('toggle');
        })
    }

    confirmdel() {
        let { delid, start } = this.state
        dz_projectdetail_del(delid, res => {
            if (res.issuccess == 1) {
              //  alert('删除成功')
                $('#myModal').modal('toggle');
                this.freshlist(start)
            } else {
                $('#myModal').modal('toggle');
                alert(res.msg);
            }
        })
    }

    closemodal() {
        $('.modal1').hide()
    }

    inputchange(prop, e) {
        this.setState({ [prop]: e.target.value })
    }
    confirm() {

      
        let{choselist,fanghao,selectid} = this.state,idarr=[];
        if(fanghao=='') {
            alert('请输入业主地址')
            return;
        }
        choselist.forEach(x=>{
            idarr.push(x.id)
        })
      //  console.log({selectid,ids:idarr.toString(),name:fanghao});

        dz_projectdetail_add({selectid,ids:idarr.toString(),name:fanghao},res=>{
            this.setState({showchose:!1})
            if(res.issuccess==1){
                this.freshlist()
            }else{
                alert(res.msg)
            }
        })
    }

    toitem(id) {
        window.location.href = './dz_detail.html?id=' + id


    //   let {roomdlist,roomlist} = this.state,item=roomdlist[i],choselist=[];
    //   let idarr = item.roomids.split(',')
    //   idarr.forEach(x=>{
    //     roomlist.forEach(y=>{
    //         if(x==y.id)choselist.push(y)
    //     })
    //   })
    //   console.log(choselist,'choselistchoselist');
    //   this.setState({showchose:!0,selectid:item.id,fanghao:item.name,choselist})
    }

    selectchange(e){
        this.setState({selectroomid:e.target.value}) 

    }


    addroom(){
        let {selectroomid,exportlist,totalamount} = this.state;
        if(Number(selectroomid)>0){
            dz_getprojectitemlist(selectroomid,res=>{
                if(res.issuccess==1){
                    let{room,wj,cp} =res;
                    let wjtotal = 0,cptotal=0,cptotalcount=0,wjtotalcount=0,amount=0
                    wj.forEach(x=>{
                        wjtotal= (Number(wjtotal)+(Number(x.singleprice)* Number(x.count))).toFixed(2)
                        wjtotalcount= (Number(wjtotalcount)+Number(x.count)).toFixed(2)
                    })
                    cp.forEach(x=>{
                        cptotalcount= (Number(cptotalcount)+Number(x.count)).toFixed(2)
                        cptotal= (Number(cptotal)+(Number(x.singleprice)* Number(x.count))).toFixed(2)
                    })
                    amount = (Number(wjtotal)+(Number(cptotal))).toFixed(2)
                    totalamount =(Number(totalamount)+(Number(amount))).toFixed(2)
                    let item = {room,wjtotal,cptotal,cptotalcount,wjtotalcount,amount,wj,cp}
                    console.log(item,'item');
                    exportlist.push(item)
                    console.log(exportlist,'exportlist');
                    this.setState({exportlist,totalamount})
                }
            })
        }
    }

    clickitem(i){
        let{roomlist,choselist} = this.state;
        choselist.push(roomlist[i])
        console.log(choselist,'choselist');
        this.setState({choselist})
        
    }
    choseitem(i){
        let{choselist} = this.state;
        choselist.splice(i,1)
        this.setState({choselist})
    }
    input(e){
        this.setState({fanghao:e.target.value})
    }

    tochi(e){

        if(confirm(`确定要转入下单报价池吗？`)){
            dz_changepoolroom({pool:'XB',date: parseInt(Date.now()/1000), id:e},res=>{
                this.freshlist()
            })
        }
    }


    render() {
        var {  showpage,fanghao,roomdlist, name,showchose,choselist, subtitle,roomlist,keyword } = this.state;
    
        return (
            <div className="breadLinebox">

                {showchose? <div className="modal_chose">

                    <div onClick={()=>{this.setState({showchose:!1})}} className="mask"></div>
                    <div className="wrap">
                    <div className="name">
                        <p>地址</p>
                        <input type="text" placeholder="请输入业主地址" value={fanghao} onChange={this.input.bind(this)} />
                    </div>
                        <div className="title">选择房间</div>
                        <div className="w">
                        <div className="left">
                            {
                                roomlist.map((x,i)=>{
                                    return(
                                        <div onClick={this.clickitem.bind(this,i)}  className={`item ${x.c?'c':''}`}>
                                            {x.name}
                                        </div>
                                    )
                                })
                            }
                        </div>
                        <div className="left r">
                        {
                                choselist.map((x,i)=>{
                                        return(
                                            <div onClick={this.choseitem.bind(this,i)}  className='item'>
                                                {x.name}
                                            </div>
                                        )
                                })
                            }

                        </div>
                        </div>
                    <div className="btns">
                        <div onClick={this.confirm.bind(this)}  className="confirm">确定</div>
                        <div onClick={()=>{this.setState({showchose:!1})}}  className="cancel">取消</div>
                    </div>
                    </div>
                </div>
                
                :null

                }


                <div className="modal1" >
                    <div onClick={this.closemodal.bind(this)} className="mask"></div>
                    <div className="wrap">
                        <div className="content">
                            <p className="p1">名称</p>
                            <input placeholder="请输入名称" allowClear value={name} onChange={this.inputchange.bind(this, 'name')} className="input" />
                            {/* <p className="p2">{zje}</p> */}
                        </div>
                        <div className="content">
                            <p className="p1">小标题</p>
                            <input placeholder="请输入小标题" allowClear value={subtitle} onChange={this.inputchange.bind(this, 'subtitle')} className="input" />
                        </div>
                        <div className="btns">
                            <p className="add" onClick={this.confirm.bind(this)}>确定</p>
                            <p className="add" onClick={this.closemodal.bind(this)}>取消</p>
                        </div>
                    </div>
                </div>

                <Modal content='确定要删除吗？' click={this.confirmdel.bind(this)} />
                <div  style={{display:showpage?'':'none'}}  className='content_wrap panel-default content_wrap'>
                    {/* <div onClick={()=>{window.location.href='./dz_cate.html'}} className='breadLine '>
                        前往分类管理
                    </div> */}
                    {/* <div className='inputs panel-body'>
                        <h3>首页管理</h3>
                    </div> */}
                    <div className="content form">
                        {/* <div style={{display:clicknew?"":"none"}} id="div"> */}

                        <div className="tabletit" >
                            
                            {/* <p className="delall"><img src="./src/delall.png"/>批量删除</p> */}
                            <p className="add" onClick={()=>{this.setState({showchose:!0})}}>新建报价</p>
                            <p className="add" onClick={()=>{window.location.href='./dz_main.html'}}>返回</p>
                            <div className="content1">
                            <input id='input' placeholder="请输入业主地址" allowClear value={keyword} onChange={this.inputchange.bind(this, 'keyword')} className="input" />
                            <p className="add" onClick={()=>{this.freshlist()}}>搜索</p>
                            {/* <p className="p2">{zje}</p> */}
                        </div>
                            {/* <p className="add" style={{'cursor':'pointer'}} onClick={()=>{window.location.href='./dz_cate.html'}}>前往分类管理</p>
                            <p className="add" style={{'cursor':'pointer'}} onClick={()=>{window.location.href='./dz_main.html'}}>客户报价</p> */}
                            {/* <p className="add" style={{'cursor':'pointer'}} onClick={()=>{window.location.href='./export.html'}}>前往导出页</p> */}
                        </div>
                        <table className="tab">
                            <thead>
                                <tr>
                                    {/* <th className="minwidth tconter"><img src="./src/notselection.png"/></th> */}
                                    <th className="tconter">序号</th>
                                    {/* <th className="tconter">修改时间</th> */}
                                    <th className="tconter">签单时间</th>
                                    <th className="tconter">归属</th>
                                    <th className="tconter">业主地址</th>
                                    <th className="cz tconter">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                {
                                    roomdlist.map((x, i) => { 
                                        return (
                                            <tr key={i}>
                                                {/* <td className="minwidth tconter"><img src="./src/notselection.png"/></td>  */}
                                                {/* <td className="tconter">{x.state=="0"?"显示":"隐藏"}</td>  */}
                                                <td className="tconter">{i + 1}</td>
                                                {/* <td className="tconter">{formatDate(new Date(x.createDate) )}</td> */}
                                                <td className="tconter">
                                              
                                              <div className="inputbox">
                                                <input type="text" id={'test'+x.id} value={x.updateDate == "0001-01-01T00:00:00" ? formatDatenotime1(new Date()) : formatDatenotime1(new Date(x.updateDate))} className="input" />
                                               </div>
                                                </td>
                                                <td className="tconter">{x.nickname}</td>
                                                <td onClick={this.edit.bind(this, x)} className="tconter">{x.name}</td>
                                                <td className="operatebox">
                                                    <div className='operate' id={"ope" + x.id}>
                                                        <p className='edit' onClick={this.toitem.bind(this,x.id)}>详情</p>
                                                        <p className='edit' onClick={this.tochi.bind(this,x.id)}>转入下单报价池</p>
                                                      
                                                        {/* <div>
                                                       <img src={`./src/${x.state=="1"?"hide":"show"}.png`}/>
                                                       <p className='edit' onClick={this.setshow.bind(this,x.id,x.state)}>{x.state=="0"?"隐藏":"显示"}</p>
                                                   </div> */}
                                                        <div style={{ display:$.cookie('permission')>49?'':'none', 'cursor': 'pointer' }}>
                                                            <img src="./src/del.png" />
                                                            <p className='edit colred' onClick={this.del.bind(this, x.id)}>删除</p>
                                                        </div>
                                                    </div>
                                                </td>
                                            </tr>
                                        )
                                    })
                                }

                            </tbody>
                        </table>
                    </div>

                </div >


            </div >

            
        )
    }
}





ReactDOM.render(
    <div>
        {/* <LeftMenu code='0' /> */}
        <FirstPage />
    </div>
    //<Barrage/>
    , $('.container')[0]
)